package com.liyueheng.util



import java.io.FileInputStream
import java.sql.{Connection, DriverManager, SQLException}
import java.util.Properties

object MySQLConnect {

  // 获取 MySQL 配置
  private val url = ConfigLoader.getString("db.url")
  private val username = ConfigLoader.getString("db.username")
  private val password = ConfigLoader.getString("db.password")
  private val driver = ConfigLoader.getString("db.driver")

println(url)
  // 加载 MySQL JDBC 驱动
  Class.forName(driver)

  // 获取数据库连接
  def getConnection: Connection = {
    try {
      // 创建并返回 MySQL 连接
      DriverManager.getConnection(url, username, password)
    } catch {
      case e: SQLException =>
        e.printStackTrace()
        throw new RuntimeException("Failed to connect to MySQL database.", e)
    }
  }

  // 关闭数据库连接
  def closeConnection(connection: Connection): Unit = {
    try {
      if (connection != null && !connection.isClosed) {
        connection.close()
      }
    } catch {
      case e: SQLException =>
        e.printStackTrace()
    }
  }
}
